const userService = require("../services/userService");
const { signToken } = require("../utils/jwtToken");

class UserController {
  // 用户注册
  async register (ctx) {
    const userData = ctx.request.body;
    await userService.register(userData);

    ctx.body = {
      code: 200,
      data: null,
      message: '注册成功'
    }
  }

  // 用户登录
  async login (ctx) {
    const userData = ctx.request.body;
    const { userInfo, last_login } = await userService.login(userData);
    const token = signToken({
      user_id: userInfo.user_id,
      username: userInfo.username,
      token_created_at: last_login
    }, '30d')

    ctx.body = {
      code: 200,
      data: {
        user: {
          user_id: userInfo.user_id,
          username: userInfo.username,
          email: userInfo.email,
          phone_number: userInfo.phone_number,
          status: userInfo.status,
          role: userInfo.role,
          avatar: userInfo.avatar,
          last_login: userInfo.last_login,
          gender: userInfo.gender,
          address: userInfo.address,
          country: userInfo.country,
          language: userInfo.language,
          preferences: userInfo.preferences,
          social: userInfo.social,
          created_at: userInfo.created_at,
          updated_at: userInfo.updated_at,
        },
        token
      },
      message: '登录成功'
    }
  }

  // 更新用户信息
  async update (ctx) {
    const data = ctx.request.body;
    const user = await userService.update(data);
    ctx.body = {
      code: 200,
      data: {
        username: user.username,
        email: user.email,
        phone_number: user.phone_number,
        status: user.status,
        role: user.role,
        avatar: user.avatar,
        last_login: user.last_login,
        gender: user.gender,
        address: user.address,
        country: user.country,
        language: user.language,
        preferences: user.preferences,
        social: user.social,        
        created_at: user.created_at,
        updated_at: user.updated_at,
      },
      message: '更新成功'
    }
  }

  // 删除用户
  async delete (ctx) {
    const { user_id } = ctx.query;
    await userService.delete(user_id);
    ctx.body = {
      code: 200,
      data: null,
      message: '删除成功'
    }
  }

  // 获取单个用户
  async findOne(ctx) {
    const { user_id } = ctx.query;
    const user = await userService.findOne(user_id);
    ctx.body = {
      code: 200,
      data: user,
      message: '查询成功'
    }
  }

  // 获取所有用户
  async findAll(ctx) {
    const users = await userService.findAll();
    ctx.body = {
      code: 200,
      data: users,
      message: 'Success'
    }
  }

  // 用户名是否存在
  async isUsernameExist(ctx) {
    const { username } = ctx.query;
    const user = await userService.isUsernameExist(username);
    if (user) {
      ctx.body = {
        code: 200,
        data: true,
        message: '用户名已存在'
      }
    } else {
      ctx.body = {
        code: 200,
        data: false,
        message: '用户名可用'
      }
    }
  }

  // 重置密码
  async resetPassword(ctx) {
    const { user_id, password} = ctx.request.body;
    await userService.resetPassword(user_id, password);
    ctx.body = {
      code: 200,
      data: null,
      message: '重置密码成功'
    }
  }
}

module.exports = new UserController();